<?php
class Application_Form_LocTree extends Zend_Form
{
	public function __construct($options = null)
	{
		$this->setName('loctree');
		$this->setMethod("post");
		$this->setEnctype("multipart/form-data");

		$address = new Zend_Form_Element_Text('address');
		$address->setLabel('Address:');
		$address->setRequired(true);
		$address->setFilters(array('StringTrim','StripTags'));
		$address->setValidators(array('NotEmpty'));

		$mapper = new Application_Model_LocTreeMapper();
		
		$elements = $mapper->fetchAll();
		$tree = array();
		foreach ($elements as $element){
			$tree[$element->getLocTreeId()] = $element->getLocTreeId()."-".$element->getAddress()." (".$element->getCode().")";
		}
		$parent = new Zend_Form_Element_Select('parentId');
		$parent->setLabel('Parent:');
		$parent->addMultiOptions(array('0' => 'Choice:')+$tree);
// 		$list->class = 'energy';
// 		$list->addValidator(new Zend_Validate_Between(array('min' => 1, 'max' => 2)))
// 		->addErrorMessage('You need to select one value');
		
		$code = new Zend_Form_Element_Text('code');
		$code->setLabel('Code:');
		$code->setRequired(true);
		$code->setFilters(array('StringTrim','StripTags'));
		$code->setValidators(array('NotEmpty'));
		
		$submit = new Zend_Form_Element_Submit('submit');
		$submit->setAttrib('id', 'submitbutton')
		->setLabel('save');



		$elements = array($parent, $address, $code, $submit);
		$this->addElements($elements);
		$this->setDecorators( array( array('ViewScript', array('viewScript' => 'forms/loginform.phtml'))));
	}
	
	public function constructTree($tree, $elements)
	{
		
		$pre = '-';
		foreach ($elements as $element){
			if($element != null){
				$pre = $pre.'-';
				$this->constructTree($tree, $elements);
			}
			$tree[$element->getLocTreeId()] = $pre.$element->getAddress();
		}
	}
}